﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using AAA.MQ;
using AAA.Meta.Quote.Data;
using AAA.Web;
using System.Threading;

namespace AAA.ConsoleTest
{
    class Program
    {


        static void Main(string[] args)
        {
/*
            HtmlPageDownloader downloader = new HtmlPageDownloader();
            HtmlFileParser fileParser = new HtmlFileParser();
            try
            {
                downloader.Path = Environment.CurrentDirectory;
                downloader.FileType = FileTypeEnum.asc;
                if(downloader.DownloadPage("http://brk.tse.com.tw:8000/isin/C_public.jsp?strMode=2", "stockid.html") == false)
                    Console.WriteLine(downloader.Message);
                fileParser.ParseTable(Environment.CurrentDirectory + @"\stockid.html",
                                      Environment.CurrentDirectory + @"\stockid.csv",
                                      "/body[1]/table[2]", ",");



            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
*/

            try
            {
                IMQClient mqClient = new ActiveMQClient("tcp://apc889:61616");
                IMessage message = new DefaultMessage();
                TickData tickData;
                long lTicks;
                int iCount = 500;
                List<IMessage> lstMessage;
/*
                tickData = new TickData();
                tickData.Ticks = DateTime.Now.Ticks;
                message.IdFieldName = "Ticks";
                message.Id = tickData.Ticks;
                message.Message = tickData;

                mqClient.Send(message);
*/
                mqClient.QueueName = "TWFE_TFHTX";
                mqClient.Connect();
                lTicks = 0;

                while (iCount-- > 0)
                {
                    lstMessage = mqClient.Peek("Ticks > " + lTicks);
                    foreach (IMessage receiveMessage in lstMessage)
                    {
                        Console.WriteLine(receiveMessage.Id);
                        lTicks = ((TickInfo)receiveMessage.Message).Ticks;
                    }
                    Thread.Sleep(1000);
                }
/*
                lstMessage = mqClient.Receive("Ticks > 0");

                foreach (IMessage receiveMessage in lstMessage)
                {
                    Console.WriteLine(receiveMessage.Id);
                }
*/
                mqClient.Disconnect();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }


            /*
                        StreamReader sr = new StreamReader("Output(r1-r3).txt", Encoding.Default);
                        StreamWriter sw = new StreamWriter("Transform(20090105).txt");
                        try
                        {            
                            string strLine;
                            string strValue;
                            string[] strValues;
                            string strPreFund = "";
                            string strFund;
                            List<string> lstFund = null;
                            List<string[]> lstReturn = null; 
                            List<DateTime> lstDate = null;
                            int iDayCount = 30;
                            DateTime dtStart = DateTime.Parse("2008/11/05");
                            DateTime dtEnd = DateTime.Parse("2009/03/06");
                            DateTime dtOutputStart = DateTime.Parse("2009/01/05 00:00:00");
                            DateTime dtOutputEnd = DateTime.Parse("2009/03/05 23:59:59");
                            DateTime dtCurrent;
               
                            sw.Write("Fund\t");
                            for (int i = 0; i < iDayCount; i++)              
                                sw.Write("D" + i + "R30\t" + "D" + i + "R60\t" + "D" + i + "R90\t");
                            sw.Write("R1\tR2\tR3\t");
                            sw.WriteLine();
               
                            sr.ReadLine();

                            while ((strLine = sr.ReadLine()) != null)
                            {
                                strValues = strLine.Split('\t');
                                dtCurrent = DateTime.Parse(strValues[1]);
                                if ((dtCurrent < dtStart) || (dtCurrent > dtEnd))
                                    continue;
                                if ((strValues[2] == "") ||
                                   (strValues[3] == "") ||
                                   (strValues[4] == ""))
                                    continue;
                   
                                strFund = strLine.Substring(0, strLine.IndexOf('\t'));
                                strFund = strFund.Substring(0, strLine.IndexOf(' '));
                                strValue = "";
                                for (int i = 2; i < strValues.Length; i++)
                                    strValue += "\t" + strValues[i];
                                strValue = strValue.Substring(1);
                                if (strFund != strPreFund)
                                {
                                    lstFund = new List<string>();
                                    lstDate = new List<DateTime>();
                                    lstReturn = new List<string[]>();
                                    strPreFund = strFund;
                                }
                                lstFund.Add(strValue);
                                lstDate.Add(dtCurrent);
                                lstReturn.Add(strValues);

                                if (lstFund.Count == iDayCount)
                                {
                                    if ((lstDate[lstDate.Count - iDayCount].Ticks >= dtOutputStart.Ticks) && (lstDate[lstDate.Count - iDayCount].Ticks <= dtOutputEnd.Ticks))
                                    {
                                        sw.Write(strFund + "\t");
                                        for (int i = 0; i < lstFund.Count; i++)
                                            sw.Write(lstFund[i] + "\t");

                                        if (lstReturn.Count >= iDayCount + 90)
                                            sw.Write(lstReturn[lstReturn.Count - iDayCount - 30][2] + "\t" + lstReturn[lstReturn.Count - iDayCount - 60][3] + "\t" + lstReturn[lstReturn.Count - iDayCount - 90][4] + "\t");
                                        else if (lstReturn.Count >= iDayCount + 60)
                                            sw.Write(lstReturn[lstReturn.Count - iDayCount - 30][2] + "\t" + lstReturn[lstReturn.Count - iDayCount - 60][3] + "\t0\t");
                                        else if(lstReturn.Count >= iDayCount + 30)
                                            sw.Write(lstReturn[lstReturn.Count - iDayCount - 30][2] + "\t0\t0\t");
                                        else
                                            sw.Write("0\t0\t0\t");
                                        sw.WriteLine();
                                    }
                                    lstFund.RemoveAt(0);
                                }
                            }
                        }
                        catch(Exception ex)
                        {
                            Console.WriteLine(ex.Message + "," + ex.StackTrace);
                        }
                        finally
                        {
                            sw.Close();
                            sr.Close();
                        }
             */ 
/*
            HtmlDocument doc = new HtmlDocument();
            try
            {
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "," + ex.StackTrace);
            }
*/
        }
    }
}
